package com.felix.algorithms.easy;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * KeyboardRow
 *
 * @author xuejinhua
 * @date 2018/9/18 9:23
 */
public class KeyboardRow {


    public String[] solution(String[] words) {
        String[] strArray = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
        Map<Character, Integer> map = new HashMap<>(3);
        int i = 0;
        for (String str : strArray) {
            for (char c : str.toCharArray()) {
                map.put(c, i);
            }
            i++;
        }
        List<String> res = new LinkedList<>();
        for (String w : words) {
            if ("".equals(w)) {
                continue;
            }
            int index = map.get(w.toUpperCase().charAt(0));
            for (char c : w.toUpperCase().toCharArray()) {
                if (map.get(c) != index) {
                    index = -1;
                    break;
                }
            }
            if (index != -1) {
                res.add(w);
            }
        }
        return res.toArray(new String[0]);
    }
}
